//
//  LYQMineSettingsViewController.m
//  ZCProject
//
//  Created by Zheng on 16/11/16.
//  Copyright © 2016年 Zheng. All rights reserved.
//

#import "LYQMineSettingsViewController.h"
#import "LYQLoginChooseViewController.h"
#import "LYQLoginChooseNavigationController.h"
#import "LYQMineAccountInfoViewController.h"
#import "LYQMineAboutUsViewController.h"
#import "LYQMineNewAlertsViewController.h"

#import "LYQMineSettingsDefaultCell.h"

//#import "AppDelegate.h"

#define kLYQMineSettingsDefaultCell_ID @"kLYQMineSettingsDefaultCell_ID"

@interface LYQMineSettingsViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSArray *imageNamesArr;
@property (nonatomic, strong) NSArray *titleNamesArr;
@property (nonatomic, strong) UIAlertController *alertCtrl;

@end

@implementation LYQMineSettingsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"设置";
    
    self.listTableView.delegate = self;
    self.listTableView.dataSource = self;
    self.listTableView.scrollEnabled = NO;
    [self.listTableView registerClass:[LYQMineSettingsDefaultCell class] forCellReuseIdentifier:kLYQMineSettingsDefaultCell_ID];
    
    _imageNamesArr = @[@"s_tongzhiicon",
                       @"s_zhxxicon",
                       @"s_qchcicon",
                       @"s_abouticon",
                       @"s_tcicon"];
    
    _titleNamesArr = @[@"新消息通知",
                       @"账号信息",
                       @"清除缓存",
                       @"关于旅业圈",
                       @"退出旅业圈"];
}

#pragma mark - tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 2;
    } else {
        return 3;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LYQMineSettingsDefaultCell *cell = [tableView dequeueReusableCellWithIdentifier:kLYQMineSettingsDefaultCell_ID forIndexPath:indexPath];
    
    cell.iconImgv.image = [UIImage imageNamed:_imageNamesArr[indexPath.row+indexPath.section*2]];
    cell.titleLbl.text = _titleNamesArr[indexPath.row+indexPath.section*2];
    
    if (indexPath.section == 1) {
        cell.accessoryView = nil;
    }
    
    return cell;

}

#pragma mark - tableview delegate
#pragma mark 头视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return kWScale(10.5);
    } else {
        return kWScale(10);
    }
}

#pragma mark 单元格高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kWScale(50);
}

#pragma mark 尾视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.0001f;
}

#pragma mark 设置分割线偏移量
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0, kWScale(10), 0, 0)];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsMake(0, kWScale(10), 0, 0)];
    }
}

#pragma mark 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
#pragma mark 跳转到新消息通知界面
            LYQMineNewAlertsViewController *mnaVC = [[LYQMineNewAlertsViewController alloc] init];
            [self.navigationController pushViewController:mnaVC animated:YES];
        } else {
#pragma mark 跳转到账号信息界面
            LYQMineAccountInfoViewController *aiVC = [[LYQMineAccountInfoViewController alloc] init];
            [self.navigationController pushViewController:aiVC animated:YES];
        }
    } else {
        if (indexPath.row == 0) {
#pragma mark 清除缓存图片
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否清除缓存" message:nil preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *sAct = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                
                [[SDImageCache sharedImageCache] clearDisk];
                [self.listTableView reloadData];
                
            }];
            
            UIAlertAction *qAct = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            [alert addAction:sAct];
            [alert addAction:qAct];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self presentViewController:alert animated:YES completion:nil];
            });
            
            
        } else if (indexPath.row == 1) {
#pragma mark 跳转到关于我们界面
            //TODO:设置背景图片
            LYQMineAboutUsViewController *mauVC = [[LYQMineAboutUsViewController alloc] init];
            [self.navigationController pushViewController:mauVC animated:YES];
            
        } else {
#pragma mark 退出登录 跳转到登录界面
            //TODO:需要清除登录数据
            
            UIAlertController *alrtCtrl = [UIAlertController alertControllerWithTitle:@"是否退出登录" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
            UIAlertAction *act1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                LYQLoginChooseViewController *lcVC = [[LYQLoginChooseViewController alloc] init];
                LYQLoginChooseNavigationController *lcnCtrl = [[LYQLoginChooseNavigationController alloc] initWithRootViewController:lcVC];
                [UIApplication sharedApplication].keyWindow.rootViewController = lcnCtrl;
                

//                AppDelegate *delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
//                delegate.window.rootViewController = lcnCtrl;

            }];
            
            UIAlertAction *act2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
            [alrtCtrl addAction:act1];
            [alrtCtrl addAction:act2];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self presentViewController:alrtCtrl animated:YES completion:nil];
            });
            
        }
    }
}




#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
